<?php
namespace app\admin\controller;
use app\model\Collect as CollectModel;
use think\Db;


class Collect extends Base
{
    public function __construct(){
        $model = new CollectModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'api_url'=>['name'=>'调用链接','type'=>'text'],
            'doc_url'=>['name'=>'文档链接','type'=>'text'],
            'uuid'=>['name'=>'关键词','type'=>'text'],
            'remark'=>['name'=>'备注','type'=>'textarea'],
            'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);
    }

    public function assignSearchs(){
        $model = $this->model;

        $search_params = [
            [
                ['key'=>'name','name'=>'名称','type'=>'text'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);

        $this->assignSearchs();

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();

        $list = $list->toarray();
        $system_url = Db::name('config')->where('key','SYSTEM_URL')->value('value');
        foreach ($list['data'] as $k => $vo) {
           if (!empty($vo['api_url'])) {
               $list['data'][$k]['api_url'] = $system_url.$vo['api_url'];
           }
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }


    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>['name','uuid'],
                'nomust'=>['status','doc_url','api_url','remark']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }

            $model = $this->model;
            if ( !empty($model->where('uuid',$save_data['uuid'])->value('id')) ) {
                $this->error('关键词已存在，请不要重复添加');
            }
        }
        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        if (request()->isPost()) {
            $fields = [
                'nomust'=>['name','doc_url','status','uuid','api_url','remark']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }

            $model = $this->model;
            if ( !empty($model->where([
                ['uuid','=',$save_data['uuid']],
                ['id','<>',$id]
            ])->value('id')) ) {
                $this->error('关键词已存在，请不要重复添加');
            }
        }
        return parent::updateData($id,$save_data);
    }




   
}
